package com.cmccgz.udacs_landlord_webapp.base;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.cmccgz.baselibrary.utils.SharedPreferencesUtil;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.main.test.SelectUrlActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author: Huang
 * @Descripion: App配置
 * @date 2017/5/23 16:04
 */

public class AppConfig {

    public static final String TAG = "Udacs_landlord";

    public static final String APP_CODE = "";

    public static final String SP_MOBILE = "sp_mobile";
    public static final String SP_TOKEN = "sp_token";
    public static final String SP_IMEI = "sp_imei";
    public static final String SP_IMSI = "sp_imsi";
    public static final String SP_USER_INFO_JSON = "sp_user_info_json";
    public static final String SP_USER_TYPE = "sp_user_type";
    public static final String SP_ACCENDANT = "sp_accendant";
    public static final String SP_BASE_IP = "sp_base_ip";
    public static final String SP_DEVICES_INFO = "sp_devices_info";
    public static final String API_VERSION = "1";
    public static final String SP_IGNORE_VERSION = "sp_ignore_version";

    public static final String UUID_SERVICE = "0000ffe0-0000-1000-8000-00805f9b34fb";
    public static final String UUID_CHARACTERISTIC = "0000ffe1-0000-1000-8000-00805f9b34fb";
    public static final String UUID_DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";

    public static final String CODE_BASE = "7E";
    public static final String CODE_OPEN_CONTROL = "030300";
    public static final String CODE_OPEN_DETA_KEY = "00000004";
    public static final String CODE_OPEN_DETA = "01000000";

    public static final String CODE_OPEN_RESULT_OK_OCNTROL = "210100";


    public static void setMobile(Context context, String mobile) {
        SharedPreferencesUtil.putString(context, SP_MOBILE, mobile);
    }

    public static String getMobile(Context context) {
        return SharedPreferencesUtil.getString(context, SP_MOBILE, "");
    }

    public static void setToken(Context context, String token) {
        SharedPreferencesUtil.putString(context, SP_TOKEN, token);
    }

    public static String getToken(Context context) {
        return SharedPreferencesUtil.getString(context, SP_TOKEN, "");
    }

    public static void setUserType(Context context, String type) {
        SharedPreferencesUtil.putString(context, SP_USER_TYPE, type);
    }

    public static String getUserType(Context context) {
        return SharedPreferencesUtil.getString(context, SP_USER_TYPE, "");
    }

    public static void seAccendant(Context context, boolean isAccendant) {
        SharedPreferencesUtil.putBoolean(context, SP_ACCENDANT, isAccendant);
    }

    public static boolean getAccendant(Context context) {
        return SharedPreferencesUtil.getBoolean(context, SP_ACCENDANT, false);
    }

    public static void setIgnoreVersion(Context context, String version){
        SharedPreferencesUtil.putString(context, SP_IGNORE_VERSION, version);
    }

    public static String getIgnoreVersion(Context context) {
        return SharedPreferencesUtil.getString(context, SP_IGNORE_VERSION, "");
    }

    public static void setIMEI(Context context) {
        TelephonyManager telManage = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String imei = telManage.getDeviceId();

        if (TextUtils.isEmpty(imei) || imei.equals("null")) {
            String uuid = getUUID(context);
            if (uuid != null && uuid.length() == 32) {
                SharedPreferencesUtil.putString(context, SP_IMEI, uuid.substring(15, 30));
                return;
            }
            SharedPreferencesUtil.putString(context, SP_IMEI, "");
            return;
        }

        SharedPreferencesUtil.putString(context, SP_IMEI, imei);
    }

    public static String getIMEI(Context context) {
        return SharedPreferencesUtil.getString(context, SP_IMEI, "");
    }

    public static void setIMSI(Context context) {
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (TextUtils.isEmpty(mTelephonyMgr.getSubscriberId()) || mTelephonyMgr.getSubscriberId().equals("null")) {
            String uuid = getUUID(context);
            if (uuid != null && uuid.length() == 32) {
                SharedPreferencesUtil.putString(context, SP_IMSI, uuid.substring(0, 15));
                return;
            }
            SharedPreferencesUtil.putString(context, SP_IMSI, "");
            return;
        }
        SharedPreferencesUtil.putString(context, SP_IMSI, mTelephonyMgr.getSubscriberId());
    }

    public static String getIMSI(Context context) {
        return SharedPreferencesUtil.getString(context, SP_IMSI, "");
    }

    private static String getUUID(Context context) {

        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        final String tmDevice, tmSerial, androidId;

        tmDevice = "" + tm.getDeviceId();

        tmSerial = "" + tm.getSimSerialNumber();

        androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

        String uniqueId = deviceUuid.toString().replace("-", "");

        Logger.i("tmDevice" + tmDevice + " tmSerial=" + tmSerial + " androidId=" + androidId);

        return uniqueId;
    }

    public static List<DeviceInfoBean> getDeviceList() {
        String deviceJson = SharedPreferencesUtil.getString(GlobalContext.getInstance(), AppConfig.SP_DEVICES_INFO, "");
        List<DeviceInfoBean> list;
        if (!TextUtils.isEmpty(deviceJson)) {
            Gson gson = new Gson();
            list = gson.fromJson(deviceJson, new TypeToken<List<DeviceInfoBean>>() {
            }.getType());
        } else {
            list = new ArrayList<>();
        }
        return list;
    }

    public static String getAppUrl(){
        String appUrl = AppConfig.getBaseUrl()+"appDownload?app_id=CMCCGZ_UDACS";
        return appUrl;
    }

    public static String getBaseUrl(){
        String baseIp = getBaseIp();
        return baseIp+"/app-api/android/";
    }

    public static String getBaseIp() {
//        String baseIp =SharedPreferencesUtil.getString(GlobalContext.getInstance(), AppConfig.SP_BASE_IP);
//      String   baseIp = BaseUrl_Shengchan;
      String   baseIp = SelectUrlActivity.BaseUrl_75;
        return baseIp;
    }

    public static boolean setBaseIp(String url){
        return SharedPreferencesUtil.putString(GlobalContext.getInstance(), AppConfig.SP_BASE_IP,url);
    }
}
